<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="请假类型" prop="leaveType">
						<el-select v-model="form.leaveType" placeholder="请选择请假类型">
							<el-option :value="item.value" :label="item.label" v-for="(item, index) in leave_type" :key="index"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="天数" prop="days">
						<el-input disabled v-model="form.days" placeholder="请输入天数" />
					</el-form-item>
				</el-col>
				<el-col :span="12" class="mb20">
					<el-form-item label="开始日期" prop="startDate">
						<el-date-picker type="date" placeholder="请选择开始日期" v-model="form.startDate" :value-format="dateStr"></el-date-picker>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="起始时间" prop="startDatePart" @change="calculateDays">
						<el-select v-model="form.startDatePart" placeholder="请选择起始时间">
							<el-option :value="item.value" :label="item.label" v-for="(item, index) in day_part" :key="index"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="结束日期" prop="endDate">
						<el-date-picker type="date" placeholder="请选择结束日期" v-model="form.endDate" :value-format="dateStr"></el-date-picker>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="结束时间" prop="endDatePart">
						<el-select v-model="form.endDatePart" placeholder="请选择结束时间" @change="calculateDays">
							<el-option :value="item.value" :label="item.label" v-for="(item, index) in day_part" :key="index"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="请假理由" prop="reason">
						<el-input type="textarea" v-model="form.reason" placeholder="请输入请假理由" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="AskForLeaveDialog">
import { useDict } from '@/hooks/dict';
import { useMessage } from '@/hooks/message';
import { getObj, addObj, putObj } from '@/api/oa/askForLeave';
import { rule } from '@/utils/validate';

const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典
const { leave_type, day_part } = useDict('leave_type', 'day_part');

function generateForm() {
	return {
		id: '',
		processKey: 'leave',
		leaveType: '',
		startDate: '',
		startDatePart: '',
		endDate: '',
		endDatePart: '',
		days: 0,
		reason: '',
		userId: '',
	};
}

//计算天数
function calculateDays() {
	const startDate = new Date(form.value.startDate);
	const endDate = new Date(form.value.endDate);
	const timeDiff = endDate.getTime() - startDate.getTime();
	const diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
	let days = diffDays + 1;
	if (form.value.startDatePart == 'pm') {
		days = diffDays - 0.5;
	}
	if (form.value.endDatePart == 'am') {
		days = diffDays - 0.5;
	}
	form.value.days = days;
	if (form.value.startDate == form.value.endDate) {
		if (form.value.startDatePart == form.value.endDatePart) {
			form.value.days = 0.5;
		}
	}
}

// 提交表单数据
const form = ref(generateForm());

// 定义校验规则
const dataRules = ref({
	leaveType: [{ required: true, message: '请假类型不能为空', trigger: 'blur' }],
	startDate: [{ required: true, message: '开始日期不能为空', trigger: 'blur' }],
	startDatePart: [{ required: true, message: '起始时间不能为空', trigger: 'blur' }],
	endDate: [{ required: true, message: '结束日期不能为空', trigger: 'blur' }],
	endDatePart: [{ required: true, message: '结束时间不能为空', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.value.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取askForLeave信息
	if (id) {
		form.value.id = id;
		getaskForLeaveData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.value.id ? await putObj(form.value) : await addObj(form.value);
		useMessage().success(form.value.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getaskForLeaveData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form.value, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
